// pages/add-address/address.js
import WxValidate from "../../plugins/wxValidate"
import {addAddress,editAddress, getAddressDetail} from '../../utils/address/address'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    id:-1,
    show:false,
    name:'',
    phone:'',
    detail:'',
    is_default:false,
    regionList:[{code:-1},{code:-1},{code:-1},{code:-1}]
  },
  initValiate(){
    const rules = {
      name: {
        required: true
      },
      phone: {
        required: true,
        tel: true
      },
      province: {   // 区域信息引入自定义的方法来检测非空信息
        regionRequired: true
      },
      city: {
        regionRequired: true
      },
      county: {
        regionRequired: true
      },
      town: {
        regionRequired: true
      },
      detail: {
        required: true
      },
      
    }

    const message = {
      name: {
        required: '请输入收货人姓名'
      },
      phone: {
        required: '请输入收货人手机号',
        tel: '请输入合法的11位手机号'
      },
      province: {
        regionRequired: '省份信息不能为空！'
      },
      city: {
        regionRequired: '城市信息不能为空！'
      },
      county: {
        regionRequired: '区/县信息不能为空！'
      },
      town: {
        regionRequired: '乡镇/街道信息不能为空！'
      },
      detail: {
        required: '请输入收货地址的详细信息'
      },
      
    }

    this.wxValidate = new WxValidate(rules,message)
    console.log(WxValidate);
    // 自定义一个方法，实现对区域非空的检测
    this.wxValidate.addMethod('regionRequired', (value, params) => {
      return value !== -1
    }, '区域信息不能为空')

  },
  onPickerShow() {
    this.setData({
      show: true
    })
  },
  onClose() {
    this.setData({
      show: false
    })
  },
  async onSubmit(e){
    const {id, name, detail, phone, regionList, is_default} = this.data

    const form = {
      id, name, detail, phone, is_default, 
      province: regionList[0].code,
      city: regionList[1].code,
      county: regionList[2].code,
      town: regionList[3].code
    }
    // 表单校验
    if(!this.wxValidate.checkForm(form)) {
      const error = this.wxValidate.errorList[0].msg
      wx.showModal({
        content: error,
        showCancel: false
      })
      return 
    }
    wx.showToast({
      title: '提交中....',
      icon: 'loading',
      duration: 30000
    })

    try {
      if(id === -1) {
        // console.log(e);
        await addAddress(form)
      } else {
        await editAddress(form)
      }
  
      wx.showToast({
        title: id===-1?'新建地址成功！': '修改地址成功！',
      })

      setTimeout(() => {
        wx.navigateBack()
      }, 1000)

    }catch(e) {
      console.log(e)
      wx.showToast({
        title: '新建地址失败！',
        icon: 'error'
      })
    }
  },

  onDefaultChange(e){
    console.log(e);
    this.setData({
      is_default:e.detail.value
    })
  },
  onAddressChange({detail}){
    console.log(detail);
    this.setData({
      regionList:detail
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: async function (options) {
    this.initValiate()
    console.log(options)
    if(options && options.id) {
      this.setData({
        id: Number(options.id)
      })

      const {name, phone, detail, is_default, province, city, county, town} = await getAddressDetail(Number(options.id))
      this.setData({
        name, phone,detail,is_default, regionList: [province, city, county, town]
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})